# -*- coding: utf-8 -*-
from src.db import db_meizi

__author__ = 'DB_BOY'

import time
import requests
from bs4 import BeautifulSoup

url = "http://www.mzitu.com/all"
pic = "http://www.mzitu.com/150343"


class Info(object):
    def __init__(self, url, title):
        self.url = url
        self.title = title


''' 获取html页面'''


def downHtml(url):
    headers = {
        'User-Agent': "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.135 Safari/537.36 Edge/12.10240",
        'Connection': 'Keep-Alive',
        'Referer': "http://www.mzitu.com/150343"
    }
    return requests.get(url, headers=headers).text


''' 解析获取所有的妹子url  '''


def parseHtml(url):
    html = downHtml(url)
    soup = BeautifulSoup(html)
    list = soup.select("a[target='_blank']")
    for a in list:
        pic = a["href"].strip()
        if pic == 'http://www.mzitu.com/old':
            continue
        if pic == 'http://www.mzitu.com/app':
            continue
        html = downHtml(pic)
        num = get_pic_num(html, pic)
        for i in range(num):
            str = "{}{}{}".format(pic, "/", i + 1)
            html = downHtml(str)
            nu = get_pic(html, pic)
            if (nu == 0):
                break


''' 获取妹子所有图片的数量 '''


def get_pic_num(html, pic):
    print('get_pic_num: ', pic)
    soup = BeautifulSoup(html)
    list = soup.find("div", class_="main-tags")
    if list is None:
        return 0
    list = list.find_all('a')
    for a in list:
        write_tag(a.get_text(), pic)

    #
    list = soup.find("div", class_="pagenavi").find_all("span")
    return int(list[-2].string)


''' 获取妹子的图片 '''


def get_pic(html, pic):
    soup = BeautifulSoup(html)
    img = soup.find("div", class_="main-image").find_all("img")
    if len(img) > 0:
        # print(img[0]["src"], img[0]["alt"])
        return write_pic(img[0]["src"], pic)
    else:
        return 0


def write_tag(tag, pic):
    # print("-------------写入数据库开始--------------")
    connection = db_meizi().connectDB()
    try:
        # 获得数据库游标（游标提供了一种对从表中检索出的数据进行操作的灵活手段，就本质而言，
        # 游标实际上是一种能从包括多条数据记录的结果集中每次提取一条记录的机制。
        # 游标总是与一条SQL 选择语句相关联因为游标由结果集（可以是零条、一条或由相关的选择语句检索出的多条记录）
        # 和结果集中指向特定记录的游标位置组成。）
        with connection.cursor() as cursor:

            sql = 'select count(*) from pic_tag where tag_name =%s and list_url=%s'
            cursor.execute(sql, (tag, pic))
            result = cursor.fetchone()
            count = result[0]
            if (count == 0):
                print("**************插入: ", tag, pic)
                sql = 'insert into pic_tag(tag_name,list_url) values(%s,%s)'
                # 执行sql语句
                cursor.execute(sql, (tag, pic))
            # else:
            #     print("--------------更新: ", tag, pic)
            # sql = 'update pic_tag set tag_name=%s where list_url =%s'
            # cursor.execute(sql, (tag,pic))

            # 插入topic表
            sql = 'select count(*) from topic where topic_name =%s'
            cursor.execute(sql, (tag))
            result = cursor.fetchone()
            count = result[0]
            if (count == 0):
                print("**************插入: ", tag)
                sql = 'insert into topic(topic_name) values(%s)'
                # 执行sql语句
                cursor.execute(sql, (tag))
            # else:
            #     print("--------------更新: ", tag)
            # sql = 'update pic_list set file_path=%s where leet_no =%s'
            # cursor.execute(sql, (info.path, info.id))
        # 事务提交
        connection.commit()
        cursor.close()
    finally:
        # 关闭数据库连接
        connection.close()
        # print("-------------写入数据库结束--------------")


def write_pic(url, pic):
    # url: 图片链接 pic:套图地址
    connection = db_meizi().connectDB()
    try:
        # 获得数据库游标（游标提供了一种对从表中检索出的数据进行操作的灵活手段，就本质而言，
        # 游标实际上是一种能从包括多条数据记录的结果集中每次提取一条记录的机制。
        # 游标总是与一条SQL 选择语句相关联因为游标由结果集（可以是零条、一条或由相关的选择语句检索出的多条记录）
        # 和结果集中指向特定记录的游标位置组成。）
        with connection.cursor() as cursor:

            sql = 'select count(*) from pic_info where list_url =%s and info_url =%s'
            cursor.execute(sql, (pic, url))
            result = cursor.fetchone()
            count = result[0]
            if (count == 0):
                print("**************插入: ", pic, url)
                sql = 'insert into pic_info(list_url,info_url) values(%s,%s)'
                # 执行sql语句
                cursor.execute(sql, (pic, url))
                # 事务提交
                connection.commit()
                cursor.close()
                return 1
            else:
                cursor.close()
                return 0
    finally:
        # 关闭数据库连接
        connection.close()


# 获取所有妹子的url
# html = downHtml(url)
# parseHtml(html)

if __name__ == "__main__":
    # 获取所有妹子的url
    parseHtml("http://www.mzitu.com/old")
    parseHtml("http://www.mzitu.com/all")
    # 获取妹子所有的图片
